﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using dbutility;
using DBUtility;
using System.Collections.Generic;

namespace hf.DAL
{
    public class TeamworkDAL
    {
        public TeamworkDAL()
		{}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.TeamworkModel> GetTeamworkList(int PageSize, int PageNumber, string strWhere, out int total)
        {
            DataSet ds = PageHelper.GetList(PageSize, PageNumber, "[tb_Teamwork]", "ID", strWhere, out total);

            //实例化列表
            List<hf.Model.TeamworkModel> list = new List<hf.Model.TeamworkModel>();

            //判断ds是不是空的
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //把datatable转换成对象
                    hf.Model.TeamworkModel m = DBHelperSQL.SetModel<hf.Model.TeamworkModel>(dr);

                    if (m != null)
                        list.Add(m);
                }
            }

            return list;
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(hf.Model.TeamworkModel model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@LawyerID", SqlDbType.Int,4),
					new SqlParameter("@ProvinceID", SqlDbType.Int,4),
					new SqlParameter("@CityID", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.VarChar,100),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@Addtime", SqlDbType.DateTime)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.LawyerID;
            parameters[2].Value = model.ProvinceID;
            parameters[3].Value = model.CityID;
            parameters[4].Value = model.Title;
            parameters[5].Value = model.Content;
            parameters[6].Value = model.Addtime;

            DBHelperSQL.RunProcedure("tb_Teamwork_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public bool Update(hf.Model.TeamworkModel model)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@LawyerID", SqlDbType.Int,4),
					new SqlParameter("@ProvinceID", SqlDbType.Int,4),
					new SqlParameter("@CityID", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.VarChar,100),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@Addtime", SqlDbType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.LawyerID;
            parameters[2].Value = model.ProvinceID;
            parameters[3].Value = model.CityID;
            parameters[4].Value = model.Title;
            parameters[5].Value = model.Content;
            parameters[6].Value = model.Addtime;

            DBHelperSQL.RunProcedure("tb_Teamwork_Update", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            DBHelperSQL.RunProcedure("tb_Teamwork_Delete", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
